
#ifndef __MINILIB_H__
#define __MINILIB_H__

////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// stack_t.
struct stack_t {
    int size;
    int length;
    void **data;
};

struct stack_t *stack_create(int size);
int stack_realloc(struct stack_t *s, int size);
int stack_push(struct stack_t *s, void *d);
void *stack_pop(struct stack_t *s);
int stack_length(struct stack_t *s);
int stack_isfull(struct stack_t *s);
void stack_clear(struct stack_t *s);
void stack_destory(struct stack_t *s);

////////////////////////////////////////

// queue
struct queue_t {
    int size;
    int length;

    int head;
    int tail;

    void **data;
};

struct queue_t *queue_create(int size);
int queue_realloc(struct queue_t *q, int size);
int queue_add(struct queue_t *q, void *d);
void *queue_del(struct queue_t *q);
int queue_length(struct queue_t *q);
int queue_isfull(struct queue_t *q);
void queue_clear(struct queue_t *q);
void queue_destory(struct queue_t *q);

////////////////////////////////////////

// singal list node
struct slnode_t
{
	void *data;
    struct slnode_t *next;
};

// signal list.
struct slist_t
{
    int length;
    struct slnode_t *head, *tail;
};

// pointor for special function.
typedef int (*slist_func_t)(void *data, void *arg);

int slist_init(struct slist_t *sl);
int slist_append(struct slist_t *sl, void *d);
int slist_insert(struct slist_t *sl, void *d1, void *d2);
int slist_delete(struct slist_t *sl, void *d);
int slist_length(struct slist_t *sl);
struct slnode_t *slist_find(struct slist_t *sl, void *d);
int slist_foreach(struct slist_t *sl, slist_func_t func, void *arg);
int slist_clear(struct slist_t *sl);

////////////////////////////////////////

// double list node.
struct dlnode_t {
    void *data;
    struct dlnode_t *prov, *next;
};

// double list.
struct dlist_t {
    int length;
    struct dlnode_t *head, *tail;
};

typedef void (*dlist_func_t)(void *data, void *arg);

int dlist_init(struct dlist_t *dl);
int dlist_append(struct dlist_t *dl, void *d);
int dlist_insert(struct dlist_t *dl, void *d1, void *d2);
int dlist_delete(struct dlist_t *dl, void *d);
int dlist_length(struct dlist_t *dl);
struct dlnode_t *dlist_find(struct dlist_t *dl, void *d);
void dlist_foreach(struct dlist_t *dl, dlist_func_t func, void *a);
int dlist_clear(struct dlist_t *dl);

////////////////////////////////////////

// btree

typedef int (*btree_cmp_t)(void *data1, void *data2);
typedef void (*btree_func_t)(void *data, void *arg);

// btree node
struct btnode_t {
    void *data;
    struct btnode_t *left, *right;
};

// btree
struct btree_t {
    int length;
    struct btnode_t *root;
    btree_cmp_t cmp;
};

int btree_init(struct btree_t *bt, btree_cmp_t cmp);
struct btnode_t *btree_add(struct btree_t *bt, void *d);
int btree_delete(struct btree_t *bt, void *d);
struct btnode_t *btree_find(struct btree_t *bt, void *d);
void btree_foreach(struct btnode_t *btn, btree_func_t func, void *arg);

////////////////////////////////////////

#endif // __MINILIB_H__
